﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_Customer_ThongKeHotelCountry
    Inherits System.Web.UI.UserControl
#Region "Sub and Function"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            LoadDanhMuc()
            BindToGrid()

            btnDelete.Attributes.Add("onclick", "return confirmMultiDelete('" & btnDelete.ClientID & "');")
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If


        End If
    End Sub

    Protected Sub LoadDanhMuc()
        Using _data As New VSEntities
            Dim itm As New ListItem("--- Tất cả ---", "-1")
            Dim l = (From q In _data.Hotels Order By q.HotelName Select New With {.Title = q.HotelName, .Id = q.HotelCode}).ToList
            With ddlKhachSan
                .DataSource = l
                .DataTextField = "Title"
                .DataValueField = "Id"
                .DataBind()
                .Items.Insert(0, itm)
            End With

            Dim k = (From q In _data.UserTypes Order By q.UserTypeId Select New With {.Title = q.Title, .Id = q.UserTypeId}).ToList
            With ddlLoaiNguoiDung
                .DataSource = k
                .DataTextField = "Title"
                .DataValueField = "Id"
                .DataBind()
                .Items.Insert(0, itm)
            End With

            Dim h = (From q In _data.HotelTypes Order By q.Title Select New With {.Title = q.Title, .Id = q.HotelTypeId}).ToList
            With ddlLoaiCSLT
                .DataSource = h
                .DataTextField = "Title"
                .DataValueField = "Id"
                .DataBind()
                .Items.Insert(0, itm)
            End With


        End Using
    End Sub

    Private Sub BindToGrid(Optional ByVal iPage As Integer = 1, Optional ByVal HotelCode As String = "", _
                           Optional ByVal TuFromDate As Date = #1/1/1900#, Optional ByVal DenFromDate As Date = #1/1/2900#)

        're value Date Den


        If DenFromDate = #1/1/1900# Then
            DenFromDate = #1/1/2900#
        End If


        Using data As New VSEntities
            'So ban ghi muon the hien tren trang

            Dim intPag_Size As Int32 = drpPage_Size.SelectedValue
            '  Dim p = (From q In data.Customers Where q.HotelId = _HotelId And q.FullName.Contains(strSearch) Order By q.CustomerId Descending Select q).Skip((iPage - 1) * intPag_Size).Take(intPag_Size).ToList
            Dim arrSearch() As String = {HotelCode, TuFromDate, DenFromDate}
            ViewState("search") = arrSearch


            Dim p As List(Of uspCustomerCountry_Result) = data.uspCustomerCountry(txtMaKS.Text, TuFromDate, DenFromDate, _
                                                                                  txtPhuongXa.Text, txtQuanHuyen.Text, ddlLoaiCSLT.SelectedValue, _
                                                                                  ddlLoaiNguoiDung.SelectedValue).ToList
            Dim strKey_Name() As String = {"HotelId"}



            With grdShow
                .DataKeyNames = strKey_Name
                .DataSource = p
                .DataBind()
            End With

        End Using
    End Sub

    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub

    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)

        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='current'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub
    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        BindToGrid(lnkTile.ToolTip)
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub

    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value)
    End Sub

    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value)
    End Sub
#End Region

#Region "Event for control"

    
    
    Protected Sub grdShow_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShow.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)


            lblSTT.Text = CInt(drpPage_Size.SelectedValue) * (CInt(hidCur_Page.Value) - 1).ToString + e.Row.RowIndex + 1
            'Dim lnkbtnDelete As LinkButton = CType(e.Row.FindControl("lnkbtnDelete"), LinkButton)

            'Dim lblCheckOut As Label = CType(e.Row.FindControl("lblCheckOut"), Label)
            'Dim _ToDate = StringToDate(grdShow.DataKeys(e.Row.RowIndex)("ToDate").ToString())
            'If _ToDate <> StringToDate("01/01/1900") Then
            '    lblCheckOut.Text = _ToDate.ToString("dd/MM/yyyy")
            'End If

            'Dim lblSex As Label = CType(e.Row.FindControl("lblSex"), Label)
            'If grdShow.DataKeys(e.Row.RowIndex)("Sex") = 1 Then
            '    lblSex.Text = "Nam"
            'Else
            '    lblSex.Text = "Nữ"
            'End If



        End If
    End Sub

    Protected Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click

        ' Dim _fromDate = StringToDate(txtFromDate.Text.Trim)


        BindToGrid(1, ddlKhachSan.SelectedValue, StringToDate(txtTuFromDate.Text), StringToDate(txtDenFromDate.Text))

    End Sub



    Protected Sub drpPage_Size_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpPage_Size.SelectedIndexChanged
        BindToGrid(1, ddlKhachSan.SelectedValue, StringToDate(txtTuFromDate.Text), StringToDate(txtDenFromDate.Text))

    End Sub
#End Region

End Class
